home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 295_01.zip / BGETBF.C < prev    next >
Text File  |  1993-04-22  |  3KB  |  132 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bgetbf.c    1.2 - 89/10/31" */
  5.  
  6. #include <errno.h>
  7. /*#include <stddef.h>*/
  8. /*#include <string.h>*/
  9. #include "blkio_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      bgetbf - get a block field from a block file
  14.  
  15. SYNOPSIS
  16.      #include <blkio.h>
  17.  
  18.      int bgetbf(bp, bn, offset, buf, bufsize)
  19.      BLKFILE *bp;
  20.      bpos_t bn;
  21.      size_t offset;
  22.      void *buf;
  23.      size_t bufsize;
  24.  
  25. DESCRIPTION
  26.      The bgetbf function reads a field from block number bn in the
  27.      block file associated with BLKFILE pointer bp into buf.  The
  28.      field begins offset characters from the beginning of the block
  29.      and is bufsize characters long.  buf must point to a storage area
  30.      at least bufsize characters long.  Block numbering starts at 1.
  31.  
  32.      bgetbf will fail if one or more of the following is true:
  33.  
  34.      [EINVAL]       bp is not a valid BLKFILE pointer.
  35.      [EINVAL]       bn or bufsize is less than 1.
  36.      [EINVAL]       buf is the NULL pointer.
  37.      [BEBOUND]      offset + bufsize extends beyond the
  38.                     boundary of block bn.
  39.      [BEEOF]        There are not bn blocks in the file.
  40.      [BEEOF]        End of file encountered within block bn.
  41.      [BENOPEN]      bp is not open for reading.
  42.  
  43. SEE ALSO
  44.      bgetb, bgethf, bputbf.
  45.  
  46. DIAGNOSTICS
  47.      Upon successful completion, a value of 0 is returned.  Otherwise,
  48.      a value of -1 is returned, and errno set to indicate the error.
  49.  
  50. ------------------------------------------------------------------------------*/
  51. int bgetbf(bp, bn, offset, buf, bufsize)
  52. BLKFILE *bp;
  53. bpos_t bn;
  54. size_t offset;
  55. void *buf;
  56. size_t bufsize;
  57. {
  58.     int i = 0;
  59.     size_t bufno = 0;
  60.  
  61.     /* validate arguments */
  62.     if (!b_valid(bp) || (bn < 1) || (buf == NULL) || (bufsize < 1)) {
  63.         errno = EINVAL;
  64.         return -1;
  65.     }
  66.  
  67.     /* check if not open for reading */
  68.     if (!(bp->flags & BIOREAD)) {
  69.         errno = BENOPEN;
  70.         return -1;
  71.     }
  72.  
  73.     /* check if block boundary is crossed */
  74.     if ((offset + bufsize) > bp->blksize) {
  75.         errno = BEBOUND;
  76.         return -1;
  77.     }
  78.  
  79.     /* check if not bn blocks in file */
  80.     if (bn >= bp->endblk) {
  81.         errno = BEEOF;
  82.         return -1;
  83.     }
  84.  
  85.     /* check if not buffered */
  86.     if (bp->bufcnt == 0) {
  87.         if (b_ugetf(bp, bn, offset, buf, bufsize) == -1) {
  88.             BEPRINT;
  89.             return -1;
  90.         }
  91.         errno = 0;
  92.         return 0;
  93.     }
  94.  
  95.     /* search buffer list for block */
  96.     for (i = 1; i <= bp->bufcnt; i++) {
  97.         if ((b_block_p(bp, (size_t)i)->bn == bn)
  98.                    && (b_block_p(bp, (size_t)i)->flags & BLKREAD)) {
  99.             bufno = i;
  100.             break;
  101.         }
  102.     }
  103.  
  104.     /* if not found, use least recently used buffer */
  105.     if (bufno == 0) {
  106.         bufno = bp->least;
  107.         if (b_put(bp, bufno) == -1) {    /* flush previous contents */
  108.             BEPRINT;
  109.             return -1;
  110.         }
  111.         b_block_p(bp, bufno)->flags = 0;
  112.         b_block_p(bp, bufno)->bn = bn;
  113.         /* read block from file */
  114.         if (b_get(bp, bufno) == -1) {
  115.             BEPRINT;
  116.             return -1;
  117.         }
  118.     }
  119.  
  120.     /* copy from block buffer into buf */
  121.     memcpy(buf, ((char *)b_blkbuf(bp, bufno) + offset), bufsize);
  122.  
  123.     /* move block buffer bufno to most recently used end of list */
  124.     if (b_mkmru(bp, bufno) == -1) {
  125.         BEPRINT;
  126.         return -1;
  127.     }
  128.  
  129.     errno = 0;
  130.     return 0;
  131. }
  132.